﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace baseClass.controls
{
    public partial class test : common.control.baseUserControl
    {
        private data.baseDS.emEducationDataTable myDataTbl = new data.baseDS.emEducationDataTable();
        public test()
        {
            try
            {
                InitializeComponent();
                SetDataSource(myDataTbl);
            }
            catch (Exception er)
            {
                ErrorHandler(this, er);
            }            
        }
        public delegate void onError(object sender, Exception er);
        public event onError myOnError = null;

        private string _employeeCode = null;
        public string myEmployeeCode
        {
            get
            {
                return _employeeCode;
            }
            set
            {
                myDataTbl.Clear();
                application.dataLibs.LoadData(myDataTbl,value); 
            }
        }
        
        public virtual void LoadData() 
        {
            emEducationDetail.LoadData();
        }
        public virtual void SaveData()
        {
            for (int idx = 0; idx < myDataTbl.Count; idx++)
            {
                if (myDataTbl[idx].RowState == DataRowState.Deleted) continue;
                myDataTbl[idx].emCode = myEmployeeCode;
            }
            application.dataLibs.UpdateData(myDataTbl);
        }
        public virtual void LockEdit(bool state)
        {
            emEducationDetail.LockEdit(state);
        }
        public virtual void SetFocus()
        {
            emEducationDetail.SetFocus();
        }

        private void SetDataSource(data.baseDS.emEducationDataTable tbl)
        {
            gridDataSource.DataSource = tbl;
            //schoolListGrid.DataSource = gridDataSource;

            DataGridViewTextBoxColumn schoolNameColumn = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn gradYearColumn = new DataGridViewTextBoxColumn();

            this.schoolListGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            schoolNameColumn, gradYearColumn});

            // 
            // schoolNameColumn
            // 
            schoolNameColumn.DataPropertyName = tbl.schoolNameColumn.ColumnName;
            schoolNameColumn.HeaderText = "Trường";
            schoolNameColumn.Name = tbl.schoolNameColumn.ColumnName;
            schoolNameColumn.ReadOnly = true;
            schoolNameColumn.Width = 250;
            // 
            // gradYearColumn
            // 
            gradYearColumn.DataPropertyName = tbl.gradYearColumn.ColumnName;
            gradYearColumn.HeaderText = "Năm TN";
            gradYearColumn.Name = tbl.gradYearColumn.ColumnName;
            gradYearColumn.ReadOnly = true;

            common.system.AutoFitGridColumn(schoolListGrid, schoolNameColumn.Name);
            CurrentDataChanged();
        }
        private void CurrentDataChanged()
        {
            data.baseDS.emEducationRow row = null;
            if (this.gridDataSource.Current != null)
            {
                row = (data.baseDS.emEducationRow)((DataRowView)this.gridDataSource.Current).Row;
            }
            emEducationDetail.SetData(row);
        }
        private void ErrorHandler(object sender, Exception er)
        {
            if (myOnError == null) return;
            myOnError(sender, er);
        }

        private void gridDataSource_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                CurrentDataChanged();
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }
        private void addLineBtn_Click(object sender, EventArgs e)
        {
            try
            {
                gridDataSource.AddNew();
                if (this.gridDataSource.Current != null)
                {
                    data.baseDS.emEducationRow row = (data.baseDS.emEducationRow)((DataRowView)this.gridDataSource.Current).Row;
                    application.dataLibs.InitData(row);
                }
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }
        private void deleteLineBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.schoolListGrid.CurrentRow == null) return;
                this.schoolListGrid.Rows.RemoveAt(schoolListGrid.CurrentRow.Index);
            }
            catch (Exception er)
            {
                ErrorHandler(sender,er);
            }
        }
    }
}
